import numpy as np
from PIL import Image
# 正则表达式的库，不符合竞赛要求
import re
import jieba
# 词云的库，不符合竞赛要求
from wordcloud import WordCloud, ImageColorGenerator, STOPWORDS
import matplotlib.pyplot as plt

# 打开存放项目名称的txt文件
with open('content.txt', 'r', encoding='utf-8') as f:
    word = (f.read())
    f.close()

# 图片模板和字体
image = np.array(Image.open('ditu.jpg'))
# windows系统
# font_path = r'C:\\Windows\\fonts\\msyh.ttf'
# Mac系统
font_path = r'/System/Library/fonts/PingFang.ttc'

# 去掉英文数字标点符号，保留中文
# resultword = re.sub("[A-Za-z0-9\[\`\~\!\@\#\$\^\&\*\(\)\=\|\{\}\'\:\;\'\,\[\]\.\<\>\/\?\~\。\@\#\\\&\*\%]", "", word)
wordlist_after_jieba = jieba.cut(word)
wl_space_split = " ".join(wordlist_after_jieba)

# 设置停用词
sw = set(STOPWORDS)
sw.add("研发")
sw.add("系列")
sw.add("这里不多写了，根据自己情况添加")

# 关键一步
my_wordcloud = WordCloud(scale=4, font_path=font_path, mask=image, stopwords=sw, background_color='white',
                         max_words=100, max_font_size=60, random_state=20).generate(wl_space_split)

# matplotlib显示生成的词云
# plt.imshow(my_wordcloud)
# plt.axis("off")
# plt.show()

# 保存生成的图片
# my_wordcloud.to_file('result.jpg')

# pil方式展示生成的词云图像（如果你没有matplotlib）
image = my_wordcloud.to_image()
image.show()
